
package com.rt.schedulenew.core.config;

import com.rt.schedulenew.core.support.shiro.CORSAuthenticationFilter;
import com.rt.schedulenew.core.support.shiro.CustomRealm;
import com.rt.schedulenew.core.support.shiro.CustomSessionManager;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.cache.ehcache.EhCacheManager;
import org.apache.shiro.session.mgt.SessionManager;
import org.apache.shiro.session.mgt.eis.EnterpriseCacheSessionDAO;
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.servlet.Filter;
import java.util.LinkedHashMap;
import java.util.Map;


@Configuration
public class ShiroConfig {
    @Bean(name = {"shiroFilter"})
    public ShiroFilterFactoryBean shiroFilter() {
        ShiroFilterFactoryBean shiroFilter = new ShiroFilterFactoryBean();
        shiroFilter.setSecurityManager(securityManager());
        shiroFilter.setLoginUrl("/login");
        shiroFilter.setLoginUrl("/getToken");
        shiroFilter.setSuccessUrl("/index");
        shiroFilter.setUnauthorizedUrl("/notRole");
        Map<String, String> filterMap = new LinkedHashMap<String, String>();
        filterMap.put("/version", "anon");
        filterMap.put("/login", "anon");
        filterMap.put("/loginSSO", "anon");
        filterMap.put("/getToken", "anon");
        filterMap.put("/updateUser", "anon");
        filterMap.put("/schedule/apm/signMulti", "anon");
        filterMap.put("/schedule/apm/signItem", "anon");
        filterMap.put("/druid/**", "anon");
        filterMap.put("/services/**", "anon");
        filterMap.put("/poi/**", "anon");
        filterMap.put("/schedule/print/**", "anon");
        filterMap.put("/rtAuth/**", "anon");
        filterMap.put("/socket/**", "anon");
        filterMap.put("/upload/**", "anon");
        filterMap.put("/alarm/**", "anon");
        filterMap.put("/cxf/**", "anon");
        filterMap.put("/dict/examItem/downloadTemplate", "anon");
        filterMap.put("/public/**", "anon");

        /*放行swagger*/
        filterMap.put("/docs", "anon");
        filterMap.put("/swagger-ui.html", "anon");
        filterMap.put("/document.html", "anon");
        filterMap.put("/webjars/springfox-swagger-ui/**", "anon");
        filterMap.put("/swagger-resources/**", "anon");
        filterMap.put("/v2/api-docs", "anon");

        filterMap.put("/**", "corsAuthenticationFilter");
        shiroFilter.setFilterChainDefinitionMap(filterMap);
        Map<String, Filter> customFilterMap = new LinkedHashMap<String, Filter>();
        customFilterMap.put("corsAuthenticationFilter", new CORSAuthenticationFilter());
        shiroFilter.setFilters(customFilterMap);
        return shiroFilter;
    }

    @Bean
    public DefaultWebSecurityManager securityManager() {
        DefaultWebSecurityManager defaultSecurityManager = new DefaultWebSecurityManager();
        defaultSecurityManager.setRealm(customRealm());
        defaultSecurityManager.setSessionManager(sessionManager());
        defaultSecurityManager.setCacheManager(ehCacheManager());
        SecurityUtils.setSecurityManager(defaultSecurityManager);
        return defaultSecurityManager;
    }

    @Bean
    public CustomRealm customRealm() {
        return new CustomRealm();
    }

    @Bean
    public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor() {
        AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor = new AuthorizationAttributeSourceAdvisor();
        authorizationAttributeSourceAdvisor.setSecurityManager(securityManager());
        return authorizationAttributeSourceAdvisor;
    }

    @Bean
    public EhCacheManager ehCacheManager() {
        EhCacheManager cacheManager = new EhCacheManager();
        cacheManager.setCacheManagerConfigFile("classpath:ehcache.xml");
        return cacheManager;
    }

    @Bean
    public SessionManager sessionManager() {
        CustomSessionManager customSessionManager = new CustomSessionManager();
        customSessionManager.setSessionDAO(new EnterpriseCacheSessionDAO());
        return customSessionManager;
    }
}
